<template>
  <div class="login login_style">
    <div class="bg" v-if="false">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <h3 class="title"></h3>
        <div style="margin-bottom: 20px" align="right">
          <el-link @click="dumpUrl('login')" style="margin-bottom: 5px">登录</el-link>
          <i class="el-icon-caret-right"></i>
        </div>
        <el-form-item prop="phonenumber">
          <el-input v-model="loginForm.phonenumber" type="text" auto-complete="off" placeholder="手机号码" clearable
            maxlength="11" show-word-limit @input="inputRes" @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="电 话" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
            @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
          </el-input>
          <div class="login-code">
            <el-button size="medium" type="primary" @click="getCode" :disabled="shows">
              <span v-if="show">获取验证码</span>
              <span v-else>倒计时{{ count }}s</span>
            </el-button>
          </div>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
            @keyup.enter.native="handleLogin" show-password>
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="restPassword">
          <el-input v-model="loginForm.restPassword" type="password" auto-complete="off" placeholder="密码"
            @keyup.enter.native="handleLogin" show-password>
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-checkbox style="margin: 0px 0px 25px 0px" :checked="multiple" @change="qf(multiple)">阅读并同意用户协议、隐私政策
        </el-checkbox>
        <el-form-item style="width: 100%">
          <el-button :loading="loading" size="medium" type="primary" style="
              width: 100%;
              background: linear-gradient(54deg, #0057a1, #4087c8);
              height: 38px;
            " @click.native.prevent="handleLogin" :disabled="!multiple">
            <span style="font-weight: bold" v-if="!loading">立 即 注 册</span>
            <span v-else>注 册 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="login_logo">
      <div class="login_logo_img"></div>
      <div class="login_logo_txt_box">
        <span class="login_logo_txt">文嘟嘟</span>
      </div>
    </div>
    <div class="login_content">
    <div class="login_box">
      <div class="login_box_content">
          <div class="login_box_top">
            <h3>注册</h3>
            <el-link class="to_register" :underline="false" type="primary" @click="dumpUrl('login')">登录>></el-link>
          </div>
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
            <el-form-item prop="phonenumber" class="form-item">
              <div class="item_form">
                <h4 class="item_form_label"><span>手</span> <span>机</span><span>号</span><span>码</span></h4>
                <el-input v-model="loginForm.phonenumber" class="login_input" type="text" auto-complete="off" placeholder="请输入手机号码" clearable
                  maxlength="11" show-word-limit @input="inputRes" @keyup.enter.native="handleLogin">
                </el-input>
              </div>
            </el-form-item>
            <el-form-item prop="code" class="form-item">
              <div class="item_form">
                <h4 class="item_form_label"><span>验</span><span>证</span><span>码</span></h4>
                <el-input v-model="loginForm.code" class="login_input" auto-complete="off" placeholder="请输入验证码" style="width: 63%"
                  @keyup.enter.native="handleLogin">
                </el-input>
                <div class="login-code">
                  <el-button size="medium" type="primary" @click="getCode" :disabled="shows">
                    <span v-if="show">获取验证码</span>
                    <span v-else>倒计时{{ count }}s</span>
                  </el-button>
                </div>
              </div>
            </el-form-item>
            <el-form-item prop="password" class="form-item">
              <div class="item_form">
                <h4 class="item_form_label"><span>密</span><span>码</span></h4>
                <el-input v-model="loginForm.password" class="login_input" type="password" auto-complete="off" placeholder="请输入密码"
                  @keyup.enter.native="handleLogin" show-password>
                </el-input>
              </div>
            </el-form-item>
            <el-form-item prop="restPassword" class="form-item">
              <div class="item_form">
                <h4 class="item_form_label"><span>确</span><span>认</span><span>密</span><span>码</span></h4>
                <el-input v-model="loginForm.restPassword" class="login_input" type="password" auto-complete="off" placeholder="请输入密码"
                  @keyup.enter.native="handleLogin" show-password>
                </el-input>
              </div>
            </el-form-item>
            <el-checkbox style="margin: 0px 0px 25px 0px" :checked="multiple" @change="qf(multiple)"> <span style="font-size: 16px;color: #5DA9EF">阅读并同意用户协议、隐私政策</span></el-checkbox>
            <el-form-item style="width: 100%">
              <el-button :loading="loading" size="medium" type="primary" style="
                  width: 100%;
                  height: 46px;
                  font-size: 18px;
                  margin-top: 35px;
                " @click.native.prevent="handleLogin" :disabled="!multiple">
                <span v-if="!loading">立 即 注 册</span>
                <span v-else>注 册 中...</span>
              </el-button>
            </el-form-item>
          </el-form>
      </div>
    </div>
    </div>

    <!--  底部  -->
    <div class="el-login-footer">
      <Footer />
    </div>
  </div>
</template>

<script>
  import {
    register
  } from "@/api/login";
  import {
    sendCode
  } from "@/api/platform/config";
  import Cookies from "js-cookie";
  const TIME_COUNT = 60;
  export default {
    name: "Register",
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请输入密码"));
        } else if (value.length < 6 || 16 < value.length) {
          callback(new Error("密码长度要小于等于16位并且大于等于6位"));
        } else {
          if (this.loginForm.restPassword !== "") {
            this.$refs.loginForm.validateField("restPassword");
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请再次输入密码"));
        } else if (value !== this.loginForm.password) {
          callback(new Error("两次输入密码不一致!"));
        } else {
          callback();
        }
      };
      var validatephone = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请输入手机号码"));
          this.shows = true;
        } else if (value.length !== 11) {
          callback(new Error("手机号码格式不正确"));
          this.shows = true;
        } else {
          callback();
          if (this.show) {
            this.shows = false;
          }
        }
      };
      return {
        codeUrl: "",
        cookiePassword: "",
        loginForm: {
          phonenumber: "",
          password: "",
          restPassword: "",
          rememberMe: false,
          code: "",
          uuid: "",
        },

        timer: null,
        show: true,
        shows: true,
        count: "",
        multiple: false,
        loginRules: {
          phonenumber: [{
            validator: validatephone,
            trigger: "blur",
          }, ],
          password: [{
            validator: validatePass,
            trigger: "blur",
          }, ],
          restPassword: [{
            validator: validatePass2,
            trigger: "blur",
          }, ],
          code: [{
            required: true,
            trigger: "blur",
            message: "验证码不能为空",
          }, ],
        },
        loading: false,
        redirect: undefined,
      };
    },
    watch: {
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true,
      },
    },
    created() {},
    methods: {
      inputRes(i) {
        i = i.replace(/^(0+)|[^\d]+/g, "");
        if (i.length == 11) {
          this.shows = false;
        } else {
          this.shows = true;
        }
      },
      qf(obj) {
        this.multiple = !obj;
      },
      dumpUrl(ul) {
        let url = window.location.href;
        let arrS = url.split("register");
        location.href = arrS[0] + ul;
      },
      getCode() {
        if (!this.timer) {
          this.count = TIME_COUNT;
          this.shows = true;
          this.show = false;
          this.timer = setInterval(() => {
            if (this.count > 0 && this.count <= TIME_COUNT) {
              this.count--;
            } else {
              this.shows = false;
              this.show = true;
              clearInterval(this.timer);
              this.timer = null;
            }
          }, 1000);
          sendCode(this.loginForm.phonenumber).then((res) => {
            this.msgSuccess("发送成功");
          });
        }
      },

      handleLogin() {

        this.$refs.loginForm.validate((valid) => {
          if (valid) {
            this.loading = true;
            this.shows = true;
            register(this.loginForm)
              .then((res) => {
                this.msgSuccess("注册成功");
                Cookies.set("username", this.loginForm.phonenumber, {
                  expires: 30,
                });
                Cookies.remove("password");
                Cookies.remove("rememberMe");
                this.$alert('您已注册成功,点击确定,跳转到登录页面！', '注册成功', {
                  confirmButtonText: '确定',
                  callback: action => {
                    this.$message({
                      type: 'info',
                      message: `action: ${ this.dumpUrl("login") }`
                    });
                  }
                });
                //this.dumpUrl("login");
              })
              .catch((error) => {
                this.loading = false;
                this.shows = false;
              });
          }
        });

      },
    },
  };
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .login {
  width: 100%;
  min-height: 100%;
  background: url("../assets/images/login_bg.png") no-repeat center;
  background-size: cover;
  position: relative;
  .login_logo {
    position: absolute;
    top: 2vh;
    left: 3vw;
    display: flex;
    align-items: center;
    .login_logo_img {
      width: 41px;
      height: 40px;
      background: url("../assets/images/login_logo.png") no-repeat center;
      margin-right: 10px;
    }
    .login_logo_txt_box {
      height: 24px;
      width: 120px;
      position: relative;
      .login_logo_txt {
        position: absolute;
        top: 0;
        left: 0;
        background: linear-gradient(54deg, #0057A1, #4087C8);
        -webkit-background-clip: text;
        color: transparent;
        -webkit-text-fill-color: transparent;
        text-fill-color: transparent;
        font-size: 24px;
        line-height: 1;
        font-weight: bold;
      }
    }
  }
}
.login_content {
  height: 88vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login_box {
  width: 480px;
  height: auto;
  background-color: #FFF;
  box-shadow: 0px 0px 40px 0px rgba(27, 46, 107, 0.05);
  border-radius: 20px;
  padding: 74px 0 36px;
  .login_box_content {
    width: 334px;
    height: auto;
    margin: 0 auto;
    .login_box_top {
      display: flex;
      align-items: flex-end;
      justify-content: space-between;
      margin-bottom: 30px;
      h3 {
        color: #5DA9EF;
        font-weight: bold;
        font-size: 30px;
        line-height: 1;
      }
      .to_register {
        line-height: 1;
        font-size: 19px;
        color: #999;
      }
    }
    .form-item {
      border-bottom: 1px solid #eee;
    }
    .item_form {
      display: flex;
      align-items: center;
      .item_form_label {
        display: flex;
        justify-content: space-between;
        width: 70px;
        color: #666;
        font-size: 18px;
        line-height: 1;
        font-weight: normal;
        line-height: 50px;
      }
      .login_input {
        padding:0;
        border:none;
        outline: none;
        flex: 1;
        overflow: hidden;
        height: 50px;
        font-size: 16px;
        color: #333;
        padding: 0 16px 0 40px;
      }
    }
  }
}
  
  .bg {
    width: 1168px;
    height: 635px;
    //background-image: url("../assets/images/Signbg.png");
    position: relative;
    margin-bottom: 80px;
  }

  .title {
    width: 286px;
    height: 56px;
    //background-image: url("../assets/images/youxiao.png");
    margin: 0px auto;
    text-align: center;
  }

  .login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    position: absolute;
    top: 105px;
    right: 100px;

    .el-input {
      height: 38px;

      input {
        height: 38px;
      }
    }

    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }

  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }

  .login-code {
    height: 38px;
    display: inline-block;
    position: relative;
    top: -2px;

    img {
      cursor: pointer;
      vertical-align: middle;
    }
  }

  .el-login-footer {
    letter-spacing: 1px;
    margin-top: 10px;
  }

  .login-code-img {
    height: 38px;
  }
</style>
